﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
using HZH_Controls.Forms;
using Clip.DataAccess;
using static System.Windows.Forms.DataFormats;

namespace HangzhouChipProject
{
    public partial class MainWindows : FrmBase
    {

        Point LeftDown;                //当前鼠标按下的坐标位置
        InterLayer interlayer;         //中间代码
        HTuple hsmartwindows;         //当前 显示窗口
        HWindow hWindowDumpControl21;  //保存图像窗口
        cReataModel creatamodel;
        findModel findmodel;
        IoCard iocard;
        FrmCamera cameraForm;
        Calibration calibration;
        frm_Gohome gohome;
        Frm_IOsignalControls IOsignalControls ;
        productClass productClass;
        int SumProNg;
        short CORE=1;
        string Path = "ROI/pruductRoi";
        HSmartWindowControl Hsmartwindows_name; //打开右键获取的窗口
                                                //自动运行
        mc.TTrapPrm trapprm1;
        mc.TTrapPrm trapprm2;
        mc.TTrapPrm trapprm3;
        mc.TTrapPrm trapprm4;
        mc.TTrapPrm trapprm5;
        mc.TTrapPrm trapprm6;
        mc.TTrapPrm trapprm7;
        mc.TTrapPrm trapprm8;

        BaslerCamera baslerCamera;
        public MainWindows()
        {
          
            InitializeComponent();

      



            LeftDown = new Point();
            interlayer = new InterLayer(hSmartWindowControl1);
            IOsignalControls = new Frm_IOsignalControls();
            // hsmartwindowsl = hSmartWindowControl1.HalconWindow;
            gohome = new frm_Gohome();
             hWindowDumpControl21 = hWindowDumpControl2.HalconWindow;
            Frm_IOsignalControls.AutoRunEventHandler += IOsignalControls_AutoRunEventHandler;
            Frm_IOsignalControls.BackHomeHandler += Frm_IOsignalControls_BackHomeHandler;
        }



        /// <summary>
        /// 轴状态
        /// </summary>
        enum MyEnum
        {
            驱动器报警,
            跟随误差越限标志,
            正限位触发标志,
            负限位触发标志,
            IO平滑停止触发标志,
            IO急停触发标志,
            电机使能,
            规划运动标志,
            电机到位标志,
        }

        double vel1, vel2, vel3, vel4, vel5, vel6, vel7, vel8;

        int poseone1, posetwo1, pose2, poseone3, posetwo3, poseone4, posetwo4, poseone5, posetwo5, poseone6, posetwo6, poseone7, posetwo7, pose8;
        /// <summary>
        /// 自动运行之前需要检查各轴的状态
        /// </summary>
        /// <summary>
        /// 检查各轴的状态
        /// </summary>
        /// <param name="axis"></param>
        /// <returns></returns>

        private bool CheckAllAxiState(short axis)
        {
            int state;
            mc.GTN_ClrSts(CORE, axis, 1);
            mc.GTN_GetSts(CORE, axis, out state, 1, out uint pc);
            if ((state & 0x2) != 0)
            {
                MessageBox.Show(axis + "号轴" + MyEnum.驱动器报警.ToString(), "消息提示框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            else if ((state & 0x10) != 0)
            {
                MessageBox.Show(axis + "号轴" + MyEnum.跟随误差越限标志.ToString(), "消息提示框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if ((state & 0x20) != 0)
            {
                MessageBox.Show(axis + "号轴" + MyEnum.正限位触发标志.ToString(), "消息提示框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if ((state & 0x40) != 0)
            {
                MessageBox.Show(axis + "号轴" + MyEnum.负限位触发标志.ToString(), "消息提示框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if ((state & 0x80) != 0)
            {
                MessageBox.Show(axis + "号轴" + MyEnum.IO平滑停止触发标志.ToString(), "消息提示框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if ((state & 0x100) != 0)
            {
                MessageBox.Show(axis + "号轴" + MyEnum.IO急停触发标志.ToString(), "消息提示框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            //else if ((state & 0x200) == 1)
            //{
            //    MessageBox.Show(MyEnum.电机使能.ToString(), "消息提示框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}
            //else if ((state & 0x400) == 0)
            //{
            //    MessageBox.Show(MyEnum.规划运动标志.ToString(), "消息提示框", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            //}
            //else if ((state & 0x800) == 1)
            //{
            //    MessageBox.Show(MyEnum.电机到位标志.ToString(), "消息提示框", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            //}
            return true;
        }
        private void ucBtnImg2_BtnClick(object sender, EventArgs e)
        {
            InterLayer.MyAxisData myAxisData = new InterLayer.MyAxisData();

            InterLayer.GetNodeInnerText(1, ref myAxisData);

            trapprm1.acc = Convert.ToDouble(myAxisData.Acc);
            trapprm1.dec = Convert.ToDouble(myAxisData.Dec);
            trapprm1.smoothTime = short.Parse(myAxisData.Smooth);
            vel1 = Convert.ToDouble(myAxisData.Vel);
            poseone1 = Convert.ToInt32(myAxisData.Poseone);
            posetwo1 = Convert.ToInt32(myAxisData.Posetwo);
         
                InterLayer.GetNodeInnerText(5, ref myAxisData);
                trapprm5.acc = Convert.ToDouble(myAxisData.Acc);
                trapprm5.dec = Convert.ToDouble(myAxisData.Dec);
                trapprm5.smoothTime = short.Parse(myAxisData.Smooth);
                vel5 = Convert.ToDouble(myAxisData.Vel);
                poseone5 = Convert.ToInt32(myAxisData.Poseone);
                posetwo5 = Convert.ToInt32(myAxisData.Posetwo);

                InterLayer.GetNodeInnerText(6, ref myAxisData);
                trapprm6.acc = Convert.ToDouble(myAxisData.Acc);
                trapprm6.dec = Convert.ToDouble(myAxisData.Dec);
                trapprm6.smoothTime = short.Parse(myAxisData.Smooth);
                vel6 = Convert.ToDouble(myAxisData.Vel);
                poseone6 = Convert.ToInt32(myAxisData.Poseone);
                posetwo6 = Convert.ToInt32(myAxisData.Posetwo);
            InterLayer.GetNodeInnerText(7, ref myAxisData);
            trapprm7.acc = Convert.ToDouble(myAxisData.Acc);
            trapprm7.dec = Convert.ToDouble(myAxisData.Dec);
            trapprm7.smoothTime = short.Parse(myAxisData.Smooth);
            vel7 = Convert.ToDouble(myAxisData.Vel);
            poseone7 = Convert.ToInt32(myAxisData.Poseone);
            posetwo7 = Convert.ToInt32(myAxisData.Posetwo);
            Parallel.Invoke(() =>
            {

                MotorsControl.PointMove(1, 1, trapprm1, vel1, poseone1);
            }, () =>
            {
                MotorsControl.PointMove(1, 5, trapprm5, vel5, poseone5);
            }, () =>
            {
                MotorsControl.PointMove(1, 6, trapprm6, vel6, poseone6);
            });

            MotorsControl.PointMove(1, 7, trapprm7, vel7, poseone7);

        }

        public void AutoRuning()
        {
            int s1 = -1, s2, s3, s4, s5, s6, s7, s8;
            bool IsError = false;
            for (short i = 1; i < 9; i++)
            {
                IsError = CheckAllAxiState(i);
            }
            if (IsError != true)
            {
                return;
            }
            InterLayer.MyAxisData myAxisData = new InterLayer.MyAxisData();
            InterLayer.MyAxisData myAxisData2 = new InterLayer.MyAxisData();

            InterLayer.GetNodeInnerText(1, ref myAxisData);
            trapprm1.acc = Convert.ToDouble(myAxisData.Acc);
            trapprm1.dec = Convert.ToDouble(myAxisData.Dec);
            trapprm1.smoothTime = short.Parse(myAxisData.Smooth);
            vel1 = Convert.ToDouble(myAxisData.Vel);
            poseone1 = Convert.ToInt32(myAxisData.Poseone);
            posetwo1 = Convert.ToInt32(myAxisData.Posetwo);

            InterLayer.GetNodeInnerText(2, ref myAxisData2);
            trapprm2.acc = Convert.ToDouble(myAxisData2.Acc);
            trapprm2.dec = Convert.ToDouble(myAxisData2.Dec);
            trapprm2.smoothTime = short.Parse(myAxisData2.Smooth);
            vel2 = Convert.ToDouble(myAxisData2.Vel);
            pose2 = Convert.ToInt32(myAxisData2.Poseone);

            InterLayer.GetNodeInnerText(5, ref myAxisData);
            trapprm5.acc = Convert.ToDouble(myAxisData.Acc);
            trapprm5.dec = Convert.ToDouble(myAxisData.Dec);
            trapprm5.smoothTime = short.Parse(myAxisData.Smooth);
            vel5 = Convert.ToDouble(myAxisData.Vel);
            poseone5 = Convert.ToInt32(myAxisData.Poseone);
            posetwo5 = Convert.ToInt32(myAxisData.Posetwo);

         

            InterLayer.GetNodeInnerText(6, ref myAxisData);
            trapprm6.acc = Convert.ToDouble(myAxisData.Acc);
            trapprm6.dec = Convert.ToDouble(myAxisData.Dec);
            trapprm6.smoothTime = short.Parse(myAxisData.Smooth);
            vel6 = Convert.ToDouble(myAxisData.Vel);
            poseone6 = Convert.ToInt32(myAxisData.Poseone);
            posetwo6 = Convert.ToInt32(myAxisData.Posetwo);

            InterLayer.GetNodeInnerText(7, ref myAxisData);
            trapprm7.acc = Convert.ToDouble(myAxisData.Acc);
            trapprm7.dec = Convert.ToDouble(myAxisData.Dec);
            trapprm7.smoothTime = short.Parse(myAxisData.Smooth);
            vel7 = Convert.ToDouble(myAxisData.Vel);
            poseone7 = Convert.ToInt32(myAxisData.Poseone);
            posetwo7 = Convert.ToInt32(myAxisData.Posetwo);

            Parallel.Invoke(() =>
            {
                s1 = MotorsControl.PointMove(1, 1, trapprm1, vel1, poseone1);
            },
            () =>
            {
                s2 = MotorsControl.PointMove(1, 2, trapprm2, vel2, pose2);
            },() =>
            {

                MotorsControl.PointMove(1, 5, trapprm5, vel5, poseone5);
            },
            () =>
            {

                MotorsControl.PointMove(1, 6, trapprm6, vel6, poseone6);
            });
            MotorsControl.PointMove(1, 7, trapprm7, vel7, poseone7);
            if ((s1 & 0x10) != 0)
            {
                return;
            }
            else
            {
                MotorsControl.Flymove2(1, new short[] { 5, 6 }, 16, 16, trapprm5, new double[] { vel5, vel6 }, new int[] { poseone5, poseone6, posetwo6 });

                //poseone5 是起始点位 ，posetwo5 是终止点位 poseone6是偏移距离
                //MotorsControl.Flymove(1, new short[] { 5, 6 }, 7, trapprm5, new double[] { vel5, vel6 }, new int[] { poseone5, poseone6, posetwo5, posetwo6 });
                MotorsControl.PointMove(1, 1, trapprm1, vel1, posetwo1);
                InterLayer.GetNodeInnerText(3, ref myAxisData);
                trapprm3.acc = Convert.ToDouble(myAxisData.Acc);
                trapprm3.dec = Convert.ToDouble(myAxisData.Dec);
                trapprm3.smoothTime = short.Parse(myAxisData.Smooth);
                vel3 = Convert.ToDouble(myAxisData.Vel);
                poseone3 = Convert.ToInt32(myAxisData.Poseone);
                posetwo3 = Convert.ToInt32(myAxisData.Posetwo);
                InterLayer.GetNodeInnerText(4, ref myAxisData);
                trapprm4.acc = Convert.ToDouble(myAxisData.Acc);
                trapprm4.dec = Convert.ToDouble(myAxisData.Dec);
                trapprm4.smoothTime = short.Parse(myAxisData.Smooth);
                vel4 = Convert.ToDouble(myAxisData.Vel);
                poseone4 = Convert.ToInt32(myAxisData.Poseone);
                posetwo4 = Convert.ToInt32(myAxisData.Posetwo);
                InterLayer.GetNodeInnerText(8, ref myAxisData);
                trapprm8.acc = Convert.ToDouble(myAxisData.Acc);
                trapprm8.dec = Convert.ToDouble(myAxisData.Dec);
                trapprm8.smoothTime = short.Parse(myAxisData.Smooth);
                vel8 = Convert.ToDouble(myAxisData.Vel);
                pose8 = Convert.ToInt32(myAxisData.Poseone);
                MotorsControl.PickupProduct(1, new short[] { 3, 4, 8 }, trapprm3, new double[] { vel3, vel4, vel8 }, new int[] { poseone3, poseone4, pose8 });

                MotorsControl.PointMove(1, 8, trapprm8, vel8, 0);
                MotorsControl.LayDownProduct(1, new short[] { 3, 4, 8 }, trapprm3, new double[] { vel3, vel4, vel8 }, new int[] { posetwo3, posetwo4, pose8 });
                MotorsControl.PointMove(1, 8, trapprm8, vel8, pose8);
                MotorsControl.PointMove(1, 8, trapprm8, vel8, 0);
            }
        }
        private void IOsignalControls_AutoRunEventHandler(object sender, EventArgs e)
        {
            AutoRuning();
        }
        private void Frm_IOsignalControls_BackHomeHandler(object sender, EventArgs e)
        {
            gohome.backHome();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            baslerCamera = new BaslerCamera("40076873");
           
            Thread time = new Thread(PrintTime);
            time.IsBackground = true;
            time.Start();

            DeviceImage clip = new DeviceImage();
            Deviceimage_tableLayoutPanel.Controls.Add(clip,0,0);
            clip.Dock = DockStyle.Fill;
            //  tableLayoutPanel1device.Controls.Add(clip,1,0);
            PrintfLog("程序已启动");
            PrintfLog("正在初始化数据库...");
            SumProNg= chartUpdata.chartupdata(NG_Chars);
            this.label_ProSumNg.Text = SumProNg.ToString();
            PrintfLog("数据库更新完成");
            if (InitioCard()==0)
            {
               // ioSignalExdi();
                MotorServeon();
          
            };
          
        }
        private void MotorServeon()
        {
            for (short i = 1; i < 9; i++)
            {
                mc.GTN_AxisOn(1, i);

            }
            MotorsControl.ioSignalEXOMonitoring(CORE, 12, 8, 0);
            MotorsControl.ioSignalEXOMonitoring(CORE, 12, 9, 0);
        }
        private void MotorServeoff()
        {
            MotorsControl.ioSignalEXOMonitoring(CORE, 12, 8, 1);
            MotorsControl.ioSignalEXOMonitoring(CORE, 12, 9, 1);
            for (short i = 1; i < 9; i++)
            {
                mc.GTN_AxisOff(1, i);

            }
           
        }
        //初始化IO板卡
        private int InitioCard()
        {
            int sRtn= mc.GTN_Open(5,1);
            if (sRtn != 0)
            {
                PrintfLog("打开IO板卡失败");
                return -1;
            }
            mc.GTN_Reset(CORE);
            MotorsControl.MotorsInit(CORE);
            if (sRtn != 0)
            {
                PrintfLog("初始化IO板卡失败,在调用Loadconfig时");
                return -1;
            }
            return 0;
        }
        //监听外部急停，启动，复位按钮
        //private void ioSignalExdi()
        //{
        //    Task task = new Task(() => {
        //        try
        //        {
        //            while (true)
        //            {
        //                int MainioState = MotorsControl.ioSignalEXIMonitoring(1, 4);
        //                switch (MainioState)
        //                {
                                                        
        //                    case 2:                   //启动
        //                        if (true)
        //                        {
        //                         //   mc.GTN_CrdStart();
        //                        }
                               
        //                        break;
        //                    case 4:                  //复位
        //                        mc.GTN_ClrSts(1,1,8);
        //                        mc.GTN_ClrSts(2, 1, 8);
        //                        mc.GTN_Reset(1);
        //                        mc.GTN_Reset(2);
        //                        break;
        //                }
        //            }
                 
        //        }
        //        catch (Exception)
        //        {
        //        }
        //    });
        //    task.Start();
         
            
        //}
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MotorServeoff();
                mc.GTN_Reset(1);
                mc.GTN_Close();
                this.Close();
              
            }
            catch (Exception)
            {

            }
          
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
          
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
         
        }


        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
 
        }
        /// <summary>
        /// 还原
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.WindowState=this.WindowState==FormWindowState.Maximized? FormWindowState.Normal: FormWindowState.Maximized;
        }
        /// <summary>
        /// 最小化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void NG_Laber_Click(object sender, EventArgs e)
        {

        }

        private void ucBtnImg1_BtnClick(object sender, EventArgs e)
        {
            if (interlayer.OpenfileDialogPath(hsmartwindows) ==true)
            {
                FrmTips.ShowTipsSuccess(this, "图像加载成功");
            } 

        }

        private void hSmartWindowControl1_Load(object sender, EventArgs e)
        {
            
            this.hSmartWindowControl1.MouseWheel += hSmartWindowControl1.HSmartWindowControl_MouseWheel ;
            this.hSmartWindowControl2.MouseWheel += hSmartWindowControl2.HSmartWindowControl_MouseWheel;
            this.hSmartWindowControl3.MouseWheel += hSmartWindowControl3.HSmartWindowControl_MouseWheel;
        }

        private void ucBtnImg3_BtnClick(object sender, EventArgs e)
        {
            if (interlayer.iSavePictrue(hWindowDumpControl21) ==true)
            {
                FrmTips.ShowTipsSuccess(this,"图像保存成功");
            }
            else
            {
                FrmTips.ShowTipsSuccess(this, "请查看图像是否加载");
            }
        }
        /// <summary>
        /// 打开模板窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ucBtnImg_CreataModel_BtnClick(object sender, EventArgs e)
        {
            try
            {
                if (creatamodel == null)
                {
                    creatamodel = new cReataModel(hSmartWindowControl1);
                    creatamodel.Show();
                }
                else
                {
                    creatamodel.Show();
                    creatamodel.Focus();
                }
                creatamodel.FormClosed += (s, se) =>
                {
                    creatamodel = null;
                };
            }
            catch (Exception ex)
            {
                PrintfLog(ex.ToString());
            }
 
        }

        /// <summary>
        /// 显示当前日期
        /// </summary>
        private void PrintTime()
        {
            try
            {
                while (this.InvokeRequired)
                {
                   this.Invoke(new Action(() => { this.Time_LB.Text = string.Format("当前时间：{0},{1}", DateTime.Now.ToString(), System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.DayOfWeek)); }));
                }
            }
            catch (Exception)
            {
            }
           

        }

        private void 矩形ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            string log=interlayer.DrawRectange1(Hsmartwindows_name);
            PrintfLog(log);
        }
        /// <summary>
        /// 消息打印日志
        /// </summary>
        /// <param name="slog"></param>
        public void PrintfLog(string slog)
        {
         InfoPrint_TB.AppendText(string.Format("消息提示: {0},{1},{2}", DateTime.Now.ToString(), slog, Environment.NewLine));
        }

        private void 矩形2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string log = interlayer.DrawRectange2(Hsmartwindows_name);
            PrintfLog(log);
        }

        private void 圆形ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string log = interlayer.DrawCircle(Hsmartwindows_name);
            PrintfLog(log);

        }

        private void 红色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            interlayer.SetRedColor();
        }

        private void 黄色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            interlayer.SetYellowColor();
        }

        private void 绿色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            interlayer.SetGreenColor();
        }

        private void 蓝色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            interlayer.SetBlueColor();
        }

        private void 虚线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            interlayer.Setlinedashed();
        }

        private void 连续ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            interlayer.Setlinecontinuous();
        }

        private void 清除所有工具ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InterLayer.ClearallTool();
        }

        private void ucBtnImg_FindModel_BtnClick(object sender, EventArgs e)
        {
            try
            {
                if (findmodel == null)
                {
                    findmodel = new findModel(this);
                    findmodel.Show();
                }
                else
                {
                    findmodel.Show();
                    findmodel.Focus();
                }
                findmodel.FormClosed += (s, se) =>
                {
                    findmodel = null;
                };
            }
            catch (Exception ex)
            {
                PrintfLog(ex.ToString());
            }
        }

        private void ucBtnImg_ioCardControl_BtnClick(object sender, EventArgs e)
        {
            try
            {
                if (iocard == null)
                {
                    iocard = new IoCard();
                    iocard.Show();
                }
                else
                {
                    iocard.Show();
                    iocard.Focus();
                }
                iocard.FormClosed += (s, se) =>
                {
                    iocard = null;
                };
            }
            catch (Exception ex)
            {
                PrintfLog(ex.ToString());
            }

        }

        private void ucBtnImg_CameraSetting_BtnClick(object sender, EventArgs e)
        {
            try
            {
                if (cameraForm == null)
                {
                    cameraForm = new FrmCamera(this);
                    cameraForm.Show();
                }
                else
                {
                    cameraForm.Show();
                    cameraForm.Focus();
                }
                cameraForm.FormClosed += (s, se) =>
                {
                    cameraForm = null;
                                       
                };
            }
            catch (Exception ex)
            {
                PrintfLog(ex.ToString());
            }
        }

        private void ucBtnImg_Calibration_BtnClick(object sender, EventArgs e)
        {
            try
            {
                if (calibration == null)
                {
                    calibration = new Calibration(this);
                    calibration.Show();
                }
                else
                {
                    calibration.Show();
                    calibration.Focus();
                }
                calibration.FormClosed += (s, se) =>
                {
                    calibration = null;
                };
            }
            catch (Exception ex)
            {
                PrintfLog(ex.ToString());
            }
        }

        private void ucBtnImg_Text_BtnClick(object sender, EventArgs e)
        {
            SumProNg = chartUpdata.chartupdata(NG_Chars);
            this.label_ProSumNg.Text = SumProNg.ToString();
        }

        private void ucBtnImg_Add_BtnClick(object sender, EventArgs e)
        {
            int num = DataManipulation.addData();
            if (num > 0)
            {
                PrintfLog(string.Format("数据添加成功,写入 {0} 条", num));
            }
        }

        private void ucCheckBox_AutoRuning_CheckedChangeEvent(object sender, EventArgs e)
        {
            if (ucCheckBox_AutoRuning.Checked==true)
            {
                tabControlExt1.Enabled = false;
            }
            else
            {
                tabControlExt1.Enabled = true;
            }
        }

        private void ucBtnTest_BtnClick(object sender, EventArgs e)
        {
            IOsignalControls.Test();
        }

        private void ucBtnImg_gohome_BtnClick(object sender, EventArgs e)
        {
            if (gohome == null )
            {
                gohome = new frm_Gohome();
                gohome.Show();
            }
            else
            {
                gohome.Show();
                gohome.Focus();
            }
            gohome.FormClosed += ((s, se) => gohome = null);
        }

       

        private void ucBtnImg_IOSignel_BtnClick(object sender, EventArgs e)
        {
            if (IOsignalControls==null)
            {
                IOsignalControls = new Frm_IOsignalControls();
                IOsignalControls.Show();
            }
            else
            {
                IOsignalControls.Show();
                IOsignalControls.Focus();
            }
            IOsignalControls.FormClosed += ((s, se) => IOsignalControls = null);
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Left_tableLayoutPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void hSmartWindowControl2_Load(object sender, EventArgs e)
        {
            this.hSmartWindowControl2.MouseWheel += hSmartWindowControl2.HSmartWindowControl_MouseWheel;
        }

        private void hSmartWindowControl3_Load(object sender, EventArgs e)
        {
            this.hSmartWindowControl3.MouseWheel += hSmartWindowControl3.HSmartWindowControl_MouseWheel;
        }

        private void ucBtnImg_AcqImage_BtnClick(object sender, EventArgs e)
        {

        }

        private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
        {
            LeftDown.X = e.X;
            LeftDown.Y = e.Y;
        }

        private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point p = MousePosition;
                p.Offset(-LeftDown.X, -LeftDown.Y);
                Location = p;
            }
        }

        private void ucBtnImg_ProductClass_BtnClick(object sender, EventArgs e)
        {
            
            if (productClass == null)
            {
                productClass = new productClass();
                productClass.Show();
            }
            else
            {
                productClass.Show();
                productClass.Focus();
            }
            productClass.FormClosed += ((s, se) => productClass = null);
        }

        private void contextMenuStrip_DrawTool_Opening(object sender, CancelEventArgs e)
        {
             Hsmartwindows_name = (sender as ContextMenuStrip).SourceControl as HSmartWindowControl;
        }

        private void hSmartWindowControl1_Click(object sender, EventArgs e)
        {

        }

        private void 加载图像ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            hsmartwindows = Hsmartwindows_name.HalconWindow;
            if (interlayer.OpenfileDialogPath(hsmartwindows) == true)
            {
                FrmTips.ShowTipsSuccess(this, "图像加载成功");
            }
        }

        private void 保存ROIToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintfLog(interlayer.SaveROI(InterLayer.selected_drawing_object, Path));
        }

        private void 创建模板ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void hSmartWindowControl2_MouseEnter(object sender, EventArgs e)
        {

        }

        private void hSmartWindowControl2_HMouseMove(object sender, HMouseEventArgs e)
        {
            if (InterLayer.hImage != null)
            {
                InterLayer.showCross(InterLayer.hImage, Hsmartwindows_name.HalconWindow, e.Y, e.X);
            }
          
                     
        }

        private void hSmartWindowControl2_HMouseDown(object sender, HMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (InterLayer.hImage != null)
                {
                    InterLayer.Createmode(InterLayer.hImage, Hsmartwindows_name.HalconWindow,e.Y,e.X, 30, 30, 10, 5, 5);
                }
            }
          // 
        }

        private void hSmartWindowControl1_HMouseMove(object sender, HMouseEventArgs e)
        {
        }

        private void hSmartWindowControl3_HMouseMove(object sender, HMouseEventArgs e)
        {

        }
    }
}
